package atm;

import ATMproject.SqlUtil;
import ATMproject.User;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class Change extends JPanel implements ActionListener {
    private JButton backBtn;

    private MainPage frame;

    private Icon backButton = Util.getIcon("/atmimage/buttonOff.jpg");
    private Icon changeButton = Util.getIcon("/atmimage/buttonModify.jpg");

    private JTextField oldPassword = new JTextField();
    private JTextField newPassword = new JTextField();
    private JButton changeBtn = new JButton(changeButton);
    private JLabel oldLabel = new JLabel("请输入您的旧密码");
    private JLabel newLabel = new JLabel("请输入您的新密码");


    public Change(MainPage frame) {
        this.frame = frame;

        this.setLayout(null);

        this.setOpaque(false);

//        JLabel titleLabel = new JLabel("改密");
//        titleLabel.setBounds(200, 50, 100, 50);
//        this.add(titleLabel);

        backBtn = new JButton(backButton);
        backBtn.setBounds(650, 150, 100, 50);
        this.add(backBtn);
        backBtn.addActionListener(this);

        oldPassword.setBounds(300, 150, 200, 25);
        this.add(oldPassword);

        newPassword.setBounds(300, 200, 200, 25);
        this.add(newPassword);

        changeBtn.setBounds(350, 330, 100, 50);
        this.add(changeBtn);
        changeBtn.addActionListener(this);

        oldLabel.setBounds(170, 150, 150, 25);
        this.add(oldLabel);

        newLabel.setBounds(170, 200, 150, 25);
        this.add(newLabel);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (backBtn == e.getSource()) {
            frame.switchPanel("menu");
            return;
        }

//        if (frame.getUser().getPassword().equals(oldPassword.getText())) {
//            frame.getUser().setPassword(newPassword.getText());
//            FileUtil.writeObject(MainPage.PATH,frame.getUserarraylist());
//            oldPassword.setText("");
//            newPassword.setText("");
//            JOptionPane.showMessageDialog(this, "修改成功");
//            return;
//        } else {
//            JOptionPane.showMessageDialog(this, "您的旧密码输入有误");
//            oldPassword.setText("");
//            newPassword.setText("");
//        }

        if (changeBtn == e.getSource()) {
            SqlUtil db = new SqlUtil();
            User loginUser = db.getById(frame.getUser().getId());
            if (db.getById(frame.getUser().getId()).getPassword().equals(oldPassword.getText())) {
                loginUser.setPassword(newPassword.getText());
                db.update(loginUser);
                JOptionPane.showMessageDialog(this, "修改成功");
                oldPassword.setText("");
                newPassword.setText("");
                return;
            } else {
                JOptionPane.showMessageDialog(this, "您的旧密码输入有误");
                oldPassword.setText("");
                newPassword.setText("");
            }


        }
    }
}
